package com.wjy.rbac.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjy.rbac.entity.User;
import com.wjy.rbac.entity.UserRole;
import com.wjy.rbac.mapper.UserMapper;
import com.wjy.rbac.service.IRoleService;
import com.wjy.rbac.service.IUserRoleService;
import com.wjy.rbac.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ybxxszl
 * @since 2019-04-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    @Qualifier(value = "userRoleServiceImpl")
    private IUserRoleService userRoleService;

    @Autowired
    @Qualifier(value = "roleServiceImpl")
    private IRoleService roleService;

    @Override
    public void insert(User user) {
        user.setStatus(0);
        save(user);
    }

    @Override
    public void update(User user) {
        user.setStatus(0);
        updateById(user);
    }

    @Override
    public void delete(String userId) {
        removeById(userId);
    }

    @Override
    public Map<String, Object> get(String userId) {
        Map<String, Object> result = new HashMap<String, Object>();
        User user = getById(userId);
        result.put("user", user);
        List<UserRole> userRoleList = userRoleService.getByUserIdList(userId);
        List<String> roleIdList = new ArrayList<String>();
        for (UserRole userRole : userRoleList) {
            roleIdList.add(userRole.getRoleId());
        }
        List<Map<String, Object>> roleList = roleService.getByRoleIdList(roleIdList);
        result.put("roleList", roleList);
        return result;
    }

    @Override
    public User getByAccount(String username) {
        Map<String, Object> columnMap = new HashMap<String, Object>();
        columnMap.put("account", username);
        List<User> userList = (List<User>) listByMap(columnMap);
        if (userList.size() == 1) {
            return userList.get(0);
        } else {
            return null;
        }
    }

}
